package com.grapes.view;

import java.util.LinkedList;
import java.util.List;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import com.grapes.R;
import com.grapes.business.TopicsBusiness;
import com.grapes.model.Comment;
import com.grapes.widget.GrapesHeadIconImgView;
import com.grapes.widget.PullToRefreshListView;

public class GlobalTopicsActivity extends GrapesActivity {
	private static final int MSG_LOAD_TOPICS_OK = 1;
	private static final int MSG_LOAD_TOPICS_FAIL = 2;
	private PullToRefreshListView commentListView;
	private long lastUsedTimestamp;
	private List<Comment> globalTopics=new LinkedList<Comment>();
	private int selectedTopicIndex = -1;
	private TopicsAdapter adapter;

	private Handler hander = new Handler() {
		@Override
		public void dispatchMessage(Message msg) {
			super.dispatchMessage(msg);
			switch (msg.what) {
			case MSG_LOAD_TOPICS_OK:
				dismissPorgressDialog();
				adapter.notifyDataSetChanged();
				break;
			case MSG_LOAD_TOPICS_FAIL:
				dismissPorgressDialog();
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.global_topics);
		commentListView = (PullToRefreshListView) findViewById(R.id.gloabal_comment_list);
		adapter=new TopicsAdapter();
		commentListView.setAdapter(adapter);
		
		Button settingBtn = (Button) findViewById(R.id.global_topics_title_setting_btn);
		settingBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				goSettingPage();
			}
		});

		Button createNewTopicBtn = (Button) findViewById(R.id.global_topics_title_create_comment_btn);
		createNewTopicBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				createNewTopic();
			}
		});

		showProgressDialog();
		loadGlobalTopics();
	}

	private void goSettingPage() {

	}

	private void createNewTopic() {

	}
	
	private void makeTopicReply()
	{
		
	}

	private void loadGlobalTopics() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					lastUsedTimestamp = System.currentTimeMillis();
					globalTopics = TopicsBusiness.getInstance()
							.getFakedGlobalTopics(lastUsedTimestamp);
					Message msg = new Message();
					msg.what = MSG_LOAD_TOPICS_OK;
					hander.sendMessage(msg);
				} catch (Exception e) {
					Message msg = new Message();
					msg.what = MSG_LOAD_TOPICS_FAIL;
					hander.sendMessage(msg);
				}
			}
		}).start();
	}
	
	private class CellViewHolder {
		GrapesHeadIconImgView headIcon;
		TextView timestampTxt;
		TextView contentTxt;
		TextView latestReplyTxt;
		TextView replyCountTxt;
		TextView commentNameTxt;
		Button publishCommentBtn;
	}
	
	private class TopicsAdapter extends BaseAdapter
	{

		@Override
		public int getCount() {
			return globalTopics.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			CellViewHolder viewHolder;
			GrapesHeadIconImgView headIcon;
			TextView timestampTxt;
			TextView contentTxt;
			TextView replyCountTxt;
			TextView commentNameTxt;
			TextView latestReplyTxt;
			Button publishCommentBtn;

			if (convertView == null) {
				convertView = getLayoutInflater().inflate(
						R.layout.comment_item, null);
				viewHolder = new CellViewHolder();
				// TextView
				viewHolder.headIcon = (GrapesHeadIconImgView) convertView
						.findViewById(R.id.company_commentor_icon);
				viewHolder.timestampTxt = (TextView) convertView
						.findViewById(R.id.company_commentor_publish_timestamp);
				viewHolder.contentTxt = (TextView) convertView
						.findViewById(R.id.company_comment_content);
				viewHolder.replyCountTxt = (TextView) convertView
						.findViewById(R.id.company_comment_reply_count);
				viewHolder.commentNameTxt = (TextView) convertView
						.findViewById(R.id.company_commentor_name);
				viewHolder.latestReplyTxt=(TextView)convertView.findViewById(R.id.company_comment_reply_hint);
				// Button
				viewHolder.publishCommentBtn = (Button) convertView
						.findViewById(R.id.create_reply_btn);
				convertView.setTag(viewHolder);
			} else
				viewHolder = (CellViewHolder) convertView.getTag();

			headIcon = viewHolder.headIcon;
			timestampTxt = viewHolder.timestampTxt;
			contentTxt = viewHolder.contentTxt;
			replyCountTxt = viewHolder.replyCountTxt;
			commentNameTxt = viewHolder.commentNameTxt;			
			publishCommentBtn = viewHolder.publishCommentBtn;
			latestReplyTxt=viewHolder.latestReplyTxt;
			
			Comment comment = globalTopics.get(position);
			headIcon.setResourceUrl(comment.getPublisher().getIconUrl());
			timestampTxt.setText(comment.getPublishTimeStamp());
			contentTxt.setText(comment.getContent());
			replyCountTxt.setText(String.valueOf(comment.getAttchedReplies()
					.size()));
			commentNameTxt.setText(comment.getPublisher().getStudentName()
					+ " " + comment.getPublisher().getSchoolName());
			
			if(comment.getLatestActivityType()==Comment.LASTEST_ACTIVITY_TYPE_CREATE)
			{
				latestReplyTxt.setVisibility(View.GONE);
			}else
			{
				latestReplyTxt.setVisibility(View.VISIBLE);
				latestReplyTxt.setText(comment.getAttchedReplies().get(0).getReplyContent());
			}
			publishCommentBtn.setOnClickListener(new View.OnClickListener() {				
				@Override
				public void onClick(View v) {
					selectedTopicIndex=position;
					makeTopicReply();
				}
			});
			return convertView;
		}		
	}
}
